<template>
  <el-dialog title="Excel批量导入" :visible="importDialogShow" @close="closeDialog" :close-on-click-modal="false">
		<uploadExcel :on-success="handleSuccess" />
	</el-dialog>
</template>

<script>
import UploadExcel from '@/components/questions/UploadExcel.vue'
export default {
  components: { UploadExcel },
  props: ['importDialogShow'],
  methods: {
    closeDialog () {
      this.$emit('update:importDialogShow', false)
    },
    /**
		 * 上传成功后执行得函数
		 */
    async handleSuccess ({ results, header }) {
      console.log(results)
      this.importEmpExcel(results)
    },
    /**
		 * 批量上传员工
		 */
    async importEmpExcel (results) {
      const userRelations = {
        试题编号: 'number',
        学科: 'subject',
        目录: 'catalog',
        题型: 'questionType',
        题干: 'question',
        难度: 'difficulty',
        答案解析: 'answer',
        录入人: 'creator',
        录入时间: 'addDate'
      }
      const arr = []
      results.forEach(item => {
        const obj = {}
        for (const key in item) {
          let value = item[key]
          if (key === '录入时间') {
            value =	this.formatExcelDate(value)
          }
          obj[userRelations[key]] = value
        }
        arr.push(obj)
      })
      // 提交数据
      this.$message.success('批量导入员工成功')
      this.$router.back()
    },
    /**
     * 转换excel的日期格式
     */
    formatExcelDate (numb, format) {
      const time = new Date((numb) * 24 * 3600000 + 1)
      time.setYear(time.getFullYear() - 70)
      const year = time.getFullYear() + ''
      const month = time.getMonth() + 1 + ''
      const date = time.getDate() - 1 + ''
      if (format && format.length === 1) {
        return year + format + (month < 10 ? '0' + month : month) + format + (date < 10 ? '0' + date : date)
      }
      return year + '-' + (month < 10 ? '0' + month : month) + '-' + (date < 10 ? '0' + date : date)
    }
  }
}
</script>

<style>

</style>
